﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;
using Harvest.API.Model;

namespace Harvest.API
{

    public partial class Wrapper
    {

        /// <summary>
        /// Get all existing contacts for an account.
        /// </summary>
        /// <remarks>GET /contacts</remarks>
        /// <returns></returns>
        public List<Contact> GetContacts()
        {
            var data = Request(WebRequestMethodEnum.Get, "contacts");
            var xDocument = XDocument.Parse(data);
            return xDocument.ToContacts();
        }

        /// <summary>
        /// Get all existing contacts for a client.
        /// </summary>
        /// <remarks>GET /clients/#{client_id}/contacts</remarks>
        /// <param name="clientId"></param>
        /// <returns></returns>
        public List<Contact> GetClientContacts(int clientId)
        {
            var data = Request(WebRequestMethodEnum.Get, String.Format("clients/{0}/contacts", clientId));
            var xDocument = XDocument.Parse(data);
            return xDocument.ToContacts();
        }

        /// <summary>
        /// Get an existing client contact.
        /// </summary>
        /// <remarks>GET /contacts/#{contact_id}</remarks>
        /// <param name="clientId"></param>
        /// <returns></returns>
        public Contact GetContact(int clientId)
        {
            var data = Request(WebRequestMethodEnum.Get, String.Format("contacts/{0}", clientId));
            var xDocument = XDocument.Parse(data);
            return xDocument.ToContact();
        }

        /// <summary>
        /// Create a new client contact.
        /// </summary>
        /// <remarks>POST /contacts</remarks>
        /// <param name="contact"></param>
        /// <returns></returns>
        public string CreateContact(Contact contact)
        {
            var data = Request(WebRequestMethodEnum.Post, "contacts", contact.Serialize());
            return data;
        }

        /// <summary>
        /// Update an existing client contact.
        /// </summary>
        /// <remarks>PUT /contacts/#{contact_id}</remarks>
        /// <param name="contact"></param>
        /// <returns></returns>
        public string UpdateContact(Contact contact)
        {
            var data = Request(WebRequestMethodEnum.Put, String.Format("contacts/{0}", contact.Id), contact.Serialize());
            return data;
        }

        /// <summary>
        /// Delete an existing client contact.
        /// </summary>
        /// <remarks>DELETE /contacts/#{contact_id}</remarks>
        /// <param name="id"></param>
        /// <returns></returns>
        public string DeleteContact(int id)
        {
            var data = Request(WebRequestMethodEnum.Delete, String.Format("contacts/{0}", id));
            return data;
        }

    }

}
